<?php
namespace app\oem\controller;
use think\Controller;
use think\request;
use think\Db;


class LoginController extends controller
{
    public function index()
    {
        return  $this->fetch();
    }
    
    
    public function login() {
    	
		if (session('oem_id')) {
            $this->redirect('oem/index/index');
        }
		
		if(request()->isPost()){
			$oem_info = db('oem')->where('mobile',input('mobile'))->where('password',md5_salt(input('password')))->find();
			if($oem_info){
				session('oem_id', $oem_info['id']);
				session('up_oem_id', $oem_info['oem_id']);
				session('agency_id', $oem_info['agency_id']);
				session('city_id', $oem_info['city_id']);
				session('county_id', $oem_info['county_id']);
				$status='登录成功';
				return json(['code'=>200,'msg'=>$status]);
			}else{
				$status='<span style="color:red">账户名或密码错误</span>';
            	return json(['code'=>402,'msg'=>$status]);
			}
			
		}
		
		return $this->fetch();
	}
  	
  	/**
     * 修改密码
     */
    public function return_pwd() {
    	if(request()->isPost()){
    		
    		$result = check_sendcode($_POST['mobile'], $_POST['yzm'], '发送成功');

			if($result != 1) {
				$this->error('验证码错误或过期');
			}
    		$user_info = db('info_oem')->where('mobile',$_POST['mobile'])->find();
	    	if($user_info){
	    			$pwd = md5_salt($_POST['pwd']);
	    			$result = Db::table('info_oem')->where('id', $user_info['id'])->update(['password' => $pwd]);
	    			if($result){
	    				$this->success('修改成功','oem/index/login');
	    			}else{
	    				$this->error('修改失败');
	    			}
	   		}else{
	   			$this->error('用户不存在');
	   		}
    	}
    	return  $this->fetch();
    } 
  	/**
     * 退出登录
     */
    public function logout() {
        session(null);
        $this->success('退出成功', 'oem/login/index');
    } 
}
